<!-- 设备分配 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库状态" prop="storageStatus">
        <el-select
          v-model="queryParams.storageStatus"
          placeholder="请选择入库状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.storage_status"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="所属渠道"
        prop="channelId"
      >
        <div style="max-width: 500px">
          <treeselect
            v-model="queryParams.channelId"
            :options="channelOptions"
            :show-count="true"
            placeholder="请选择渠道"
            noResultsText="暂无数据"
            @input="handleQuery"
          />
        </div>
      </el-form-item>
      <el-form-item label="渠道分配" prop="channelTime">
        <el-date-picker
          v-model="channelTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="激活时间" prop="activationTime">
        <el-date-picker
          v-model="activationTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="tables"
      v-loading="loading"
      :data="equipmentList"
      @selection-change="handleSelectionChange"
      :cell-style="cellStyle"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="设备序列号"
        align="center"
        prop="serialNo"
        width="180"
        fixed="left"
      />
      <el-table-column
        label="设备终端号"
        align="center"
        prop="terminalNo"
        width="100"
      />
      <el-table-column label="型号" align="center" prop="modelNo" width="80" />
      <el-table-column
        label="所属事业部"
        align="center"
        prop="platformName"
        width="250"
      />
      <el-table-column
        label="事业部分配时间"
        align="center"
        prop="platformTime"
        width="250"
      />
      <el-table-column
        label="所属渠道"
        align="center"
        prop="channelName"
        width="250"
      />
      <el-table-column
        label="渠道分配时间"
        align="center"
        prop="channelTime"
        width="180"
      >
        <template slot-scope="scope">{{
          parseTime(scope.row.channelTime)
        }}</template>
      </el-table-column>
      <el-table-column
        label="所属单位"
        align="center"
        prop="companyName"
        width="250"
      />
      <el-table-column
        label="单位分配时间"
        align="center"
        prop="companyTime"
        width="180"
      >
        <template slot-scope="scope">{{
          parseTime(scope.row.companyTime)
        }}</template>
      </el-table-column>
      <el-table-column
        label="所属部门"
        align="center"
        prop="deptName"
        width="200"
      />
      <el-table-column
        label="部门分配时间"
        align="center"
        prop="deptTime"
        width="180"
      >
        <template slot-scope="scope">{{
          parseTime(scope.row.deptTime)
        }}</template>
      </el-table-column>
      <el-table-column
        label="设备激活时间"
        align="center"
        prop="activationTime"
        width="180"
      >
        <template slot-scope="scope">{{
          parseTime(scope.row.activationTime)
        }}</template>
      </el-table-column>
      <el-table-column
        label="设备到期时间"
        align="center"
        prop="expirationTime"
        width="180"
      >
        <template slot-scope="scope">{{
          parseTime(scope.row.expirationTime)
        }}</template>
      </el-table-column>

      <el-table-column
        label="设备续费周期(月)"
        align="center"
        prop="renewalCycle"
        width="80"
      />
      <el-table-column
        label="续费提醒倒计时(天)"
        align="center"
        prop="renewalReminder"
        width="90"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="200"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:distribute:edit']"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="型号">{{ form.modelNo }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备终端号">{{
              form.terminalNo
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="序列号">{{ form.serialNo }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属事业部" prop="platformId">
              <SelectEnterprise :defaultValue.sync="form.platformId" :disabled="userInfo.userType !== 0" @change="platformChange" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属渠道" prop="channelId">
              <treeselect
                v-model="form.channelId"
                :options="channelOptions"
                :show-count="true"
                placeholder="请选择渠道"
                noResultsText="暂无数据"
                :disabled="!form.platformId || (userInfo.userType !== 0 && userInfo.userType !== 3)"
                @input="channelIdChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属单位" prop="companyId">
              <SelectCompany
                :defaultValue.sync="form.companyId"
                :companyType="0"
                :channelId="form.channelId"
                :disabled="!form.channelId || (userInfo.userType !== 0 && userInfo.userType !== 3 && userInfo.userType !== 1)"
                @change="companyIdChange"
                class="width100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <SelectDept
                :defaultValue="form.deptId"
                :comId="form.companyId"
                :disabled="!form.companyId"
                @change="(e) => (form.deptId = e)"
                class="width100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备入口配置" prop="deskConfigArray">
              <el-select v-model="form.deskConfigArray" placeholder="请选择桌面启动入口" clearable multiple >
                <el-option v-for="item in deskConfigOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备续费周期(月)" prop="renewalCycle" required>
              <el-input-number
                v-model="form.renewalCycle"
                controls-position="right"
                :min="0"
                class="width100"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="续费提醒倒计时(天)" prop="renewalReminder" required>
              <el-input-number
                v-model="form.renewalReminder"
                controls-position="right"
                :min="0"
                class="width100"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="3"
                placeholder="请输入备注"
                v-model="form.remark"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipment,
  delEquipment,
  updateEquipment,
} from "@/api/equipment/equipment";
import { treeselect } from "@/api/channel/channel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SelectCompany from "@/components/SelectCompany";
import SelectDept from "@/components/SelectDept";
import SelectEnterprise from "@/views/enterprise/enterprise/selectEnterprise";
import { listVersion } from "@/api/saas/version";

export default {
  name: "Distribute",
  dicts: ["storage_status"],
  components: { Treeselect, SelectCompany, SelectDept, SelectEnterprise },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo;
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        params: { pageNum: 1, pageSize: 10 },
        searchType: 1,
        searchValue: null,
        storageStatus: null,
        channelId: null,
        channelTimeStart: null,
        channelTimeEnd: null,
        activationTimeStart: null,
        activationTimeEnd: null,
      },
      activationTime: "",
      channelTime: "",
      // 渠道树选项
      channelOptions: [],
      // 默认排序
      defaultSort: { prop: "id", order: "descending" },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        platformId: [
          { required: true, message: "请选择事业部", trigger: "change" },
        ],
      },
      // app列表
      deskConfigOptions: []
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  activated() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    cellStyle(row, column, rowIndex, columnIndex) {
      if (row.row.isReminder === "开启") {
        return "color:red";
      }
    },
    /** 查询归属渠道下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.channelOptions = response.data;
      });
    },
    getListVersion() {
      listVersion().then(res=>{
        this.deskConfigOptions = res.rows;
      })
    },
    /** 查询设备管理列表 */
    getList() {
      this.queryParams.channelTimeStart =
        this.channelTime && this.channelTime.length ? this.channelTime[0] : "";
      this.queryParams.channelTimeEnd =
        this.channelTime && this.channelTime.length ? this.channelTime[1] : "";
      this.queryParams.activationTimeStart =
        this.activationTime && this.activationTime.length
          ? this.activationTime[0]
          : "";
      this.queryParams.activationTimeEnd =
        this.activationTime && this.activationTime.length
          ? this.activationTime[1]
          : "";
      this.loading = true;
      listEquipment(this.queryParams)
        .then((response) => {
          this.equipmentList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        terminalNo: null,
        modelNo: null,
        storageStatus: "0",
        initTime: null,
        storageTime: null,
        remark: null,
        platformId: null,
        channelId: null,
        companyId: null,
        deptId: null,
        isActivated: null,
        channelTime: null,
        activationTime: null,
        activationStatus: "0",
        expirationTime: null,
        renewalCycle: null,
        renewalReminder: null,
        enabledStatus: "0",
        desktopVersion: null,
        appVersion: null,
        appLoginStatus: "0",
        isVideoCall: null,
        updateTime: null,
        equipmentSet: {
          id: null,
          equipmentId: null,
          userType: 0,
          renewalCycle: 0,
          renewalReminder: 0,
        }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = null;
      this.queryParams.storageStatus = null;
      this.queryParams.channelId = null;
      this.queryParams.channelTimeStart = null;
      this.queryParams.channelTimeEnd = null;
      this.queryParams.activationTimeStart = null;
      this.queryParams.activationTimeEnd = null;
      this.channelTime = "";
      this.activationTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getListVersion();
      this.reset();
      this.form = { ...row };
      this.$set(this.form, 'deskConfigArray', row.deskConfig ? JSON.parse(row.deskConfig) : []);
      delete this.form.deskConfig;
      console.log(JSON.parse(JSON.stringify(this.form)));
      this.open = true;
      this.title = "设备分配编辑";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.equipmentSet = {
            id: this.form.setId,
            renewalCycle: this.form.renewalCycle,
            renewalReminder: this.form.renewalReminder,
            equipmentId: this.form.id,
            userType: 0,
          };
          updateEquipment(this.form).then((response) => {
            this.$modal.msgSuccess("编辑成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 事业部切换
    platformChange(){
      this.form.channelId = null;
      this.form.companyId = null;
      this.form.deptId = null;
    },
    channelIdChange(e) {
      this.form.companyId = null;
      this.form.deptId = null;
    },
    companyIdChange() {
      this.form.deptId = null;
    },
  },
};
</script>
